// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        MinLifeTime:0,
        fadeInDuration:0,
        fadeOutDuration:0,
        MinSpeed:0,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.MinSpeed -= parseInt((Math.random()*100) % 50 );
        this.LifeTime =  -(this.node.parent.width / 2 + this.node.x) / this.MinSpeed
        this.fadeIn();
    },

    start () {
        
    },

    fadeIn:function(){
        this.node.runAction(cc.fadeIn(this.fadeInDuration));
    },

    fadeOut:function(){
        var callbackfun = cc.callFunc(this.callBackFun ,this) ;
        this.node.runAction(cc.sequence(cc.fadeOut(this.fadeOutDuration), callbackfun));
    },

    callBackFun:function(){
        this.node.destroy();
    },
    update (dt) {
        this.LifeTime -= dt
        this.node.x += this.MinSpeed * dt;
        if (this.LifeTime <= 0 ){
            this.fadeOut()
        }
    },
});
